package top.codedance.iotp.tools.terminal.ui;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Formatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author xuedianfu
 */
public class SetNetwork extends javax.swing.JDialog {

    private ShellFrame sf;

    /**
     * Creates new form setNetwork
     */
    public SetNetwork(ShellFrame sf) {
        super(sf, true);
        this.sf = sf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.sf);
        setMinimumSize(new Dimension(421, 286));
        setTitle("设置网络命令生成器");
    }

    public void initData(String cmd){
        if(cmd == null || cmd.isEmpty()) {
            this.jtf_devName.setText("net1");
            this.jtf_networkType.setText("static");
            this.jtf_ip.setText("192.168.1.234");
            this.jtf_netMask.setText("255.255.255.0");
            this.jtf_gateway.setText("");
            this.jtf_masterDns.setText("");
            this.jtf_backupDns.setText("");
        }else{
            Pattern p1 = Pattern.compile("netsh\\s+interface\\s+ip\\s+set\\s+address\\s+name=(\\S+)\\s+source=(\\S+)\\s+addr=(\\S+)\\s+mask=(\\S+)(?:\\s+gateway=)?(\\S*)");
            Matcher matcher1 = p1.matcher(cmd);
            if(matcher1.find()) {
                this.jtf_devName.setText(matcher1.group(1));
                this.jtf_networkType.setText(matcher1.group(2));
                this.jtf_ip.setText(matcher1.group(3));
                this.jtf_netMask.setText(matcher1.group(4));
                this.jtf_gateway.setText(matcher1.group(5));
                this.jtf_masterDns.setText("");
                this.jtf_backupDns.setText("");
                return;
            }
            Pattern p2 = Pattern.compile("netsh\\s+interface\\s+ip\\s+set\\s+dns\\s+name=(\\S+)\\s+source=(\\S+)\\s+addr=(.+)");
            Matcher matcher2 = p2.matcher(cmd);
            if(matcher2.find()) {
                this.jtf_devName.setText(matcher2.group(1));
                this.jtf_networkType.setText(matcher2.group(2));
                this.jtf_ip.setText("");
                this.jtf_netMask.setText("");
                this.jtf_gateway.setText("");
                this.jtf_masterDns.setText(matcher2.group(3));
                this.jtf_backupDns.setText("");
                return;
            }
            Pattern p3 = Pattern.compile("netsh\\s+interface\\s+ip\\s+add\\s+dns\\s+(\\S+)\\s+addr=(\\S+)\\s+index=2");
            Matcher matcher3 = p3.matcher(cmd);
            if(matcher3.find()) {
                this.jtf_devName.setText(matcher3.group(1));
                this.jtf_networkType.setText("");
                this.jtf_ip.setText("");
                this.jtf_netMask.setText("");
                this.jtf_gateway.setText("");
                this.jtf_masterDns.setText("");
                this.jtf_backupDns.setText(matcher3.group(2));
                return;
            }
            Pattern p4 = Pattern.compile("netsh\\s+interface\\s+ip\\s+set\\s+address\\s+name=(\\S+)\\s+source=(.+)");
            Matcher matcher4 = p4.matcher(cmd);
            if(matcher4.find()) {
                this.jtf_devName.setText(matcher4.group(1));
                this.jtf_networkType.setText(matcher4.group(2));
                this.jtf_ip.setText("");
                this.jtf_netMask.setText("");
                this.jtf_gateway.setText("");
                this.jtf_masterDns.setText("");
                this.jtf_backupDns.setText("");
                return;
            }
            Pattern p5 = Pattern.compile("netsh\\s+interface\\s+ip\\s+set\\s+dns\\s+name=(\\S+)\\s+source=(.+)");
            Matcher matcher5 = p5.matcher(cmd);
            if(matcher5.find()) {
                this.jtf_devName.setText(matcher5.group(1));
                this.jtf_networkType.setText(matcher5.group(2));
                this.jtf_ip.setText("");
                this.jtf_netMask.setText("");
                this.jtf_gateway.setText("");
                this.jtf_masterDns.setText("");
                this.jtf_backupDns.setText("");
                return;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtf_devName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtf_networkType = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtf_ip = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtf_netMask = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jtf_gateway = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jtf_masterDns = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jtf_backupDns = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jb_save = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("网卡名称：");
        jLabel1.setToolTipText("");

        jtf_devName.setText("net1");
        jtf_devName.setToolTipText("最好为英文名，如：net1");

        jLabel2.setText("网格类型：");
        jLabel2.setToolTipText("");

        jtf_networkType.setText("static");
        jtf_networkType.setToolTipText("值为：static 或 dhcp");

        jLabel3.setText("IP地址：");

        jtf_ip.setText("192.168.1.234");
        jtf_ip.setToolTipText("如：192.168.0.2");

        jLabel4.setText("掩码：");

        jtf_netMask.setText("255.255.255.0");
        jtf_netMask.setToolTipText("一般为：255.255.255.0");

        jLabel5.setText("网关：");

        jtf_gateway.setToolTipText("可以不设，根据情况设置");

        jLabel6.setText("IP地址设置");
        jLabel6.setToolTipText("");

        jLabel7.setText("DNS设置");
        jLabel7.setToolTipText("");

        jLabel8.setText("主DNS：");

        jtf_masterDns.setToolTipText("可以不设，根据情况设置");

        jLabel9.setText("备用DNS：");

        jtf_backupDns.setToolTipText("可以不设，根据情况设置");

        jb_save.setText("保存");
        jb_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(184, 184, 184)
                                .addComponent(jLabel6))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(22, 22, 22)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jtf_devName, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(166, 166, 166)
                                        .addComponent(jLabel7))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel2)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(jtf_networkType))
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel3)
                                                .addComponent(jLabel4)
                                                .addComponent(jLabel5))
                                            .addGap(18, 18, 18)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jtf_ip)
                                                .addComponent(jtf_netMask)
                                                .addComponent(jtf_gateway)))
                                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel8)
                                                .addComponent(jLabel9))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jtf_masterDns)
                                                .addComponent(jtf_backupDns, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(165, 165, 165)
                        .addComponent(jb_save)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtf_devName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtf_networkType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtf_ip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jtf_netMask, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jtf_gateway, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jtf_masterDns, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jtf_backupDns, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_save)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveActionPerformed
        boolean isOk = false;
        if(this.jtf_devName.getText().isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "网卡名不能为空");
            return;
        }
        if(!this.jtf_networkType.getText().equals("static") && !this.jtf_networkType.getText().equals("dhcp")){
            JOptionPane.showMessageDialog(rootPane, "网络类型只能是 static 或 dhcp");
            return;
        }
        Pattern p = Pattern.compile("^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]?\\d)$");
        Matcher matcher1 = p.matcher(this.jtf_ip.getText());
        if(this.jtf_networkType.getText().equals("static") && !matcher1.matches()){
            JOptionPane.showMessageDialog(rootPane, "IP格式不正确");
            return;
        }
        Matcher matcher2 = p.matcher(this.jtf_netMask.getText());
        if(this.jtf_networkType.getText().equals("static") && !matcher2.matches()){
            JOptionPane.showMessageDialog(rootPane, "掩码格式不正确");
            return;
        }
        Matcher matcher3 = p.matcher(this.jtf_gateway.getText());
        if(this.jtf_networkType.getText().equals("static") && !this.jtf_gateway.getText().isEmpty() && !matcher3.matches()){
            JOptionPane.showMessageDialog(rootPane, "网关格式不正确");
            return;
        }
        Matcher matcher4 = p.matcher(this.jtf_masterDns.getText());
        if(this.jtf_networkType.getText().equals("static") && !this.jtf_masterDns.getText().isEmpty() && !matcher4.matches()){
            JOptionPane.showMessageDialog(rootPane, "主DNS格式不正确");
            return;
        }
        Matcher matcher5 = p.matcher(this.jtf_backupDns.getText());
        if(this.jtf_networkType.getText().equals("static") && !this.jtf_backupDns.getText().isEmpty() && !matcher5.matches()){
            JOptionPane.showMessageDialog(rootPane, "备用DNS网关格式不正确");
            return;
        }
        if(this.jtf_networkType.getText().equals("static")) {
            if(!this.jtf_ip.getText().isEmpty() && !this.jtf_netMask.getText().isEmpty()) {
                Formatter formater1 = new Formatter();
                if(this.jtf_gateway.getText().isEmpty()){
                    formater1.format(
                            "netsh interface ip set address name=%s source=%s addr=%s mask=%s",
                            this.jtf_devName.getText(), this.jtf_networkType.getText(), this.jtf_ip.getText(), this.jtf_netMask.getText()
                    );
                }else {
                    formater1.format(
                            "netsh interface ip set address name=%s source=%s addr=%s mask=%s gateway=%s",
                            this.jtf_devName.getText(), this.jtf_networkType.getText(), this.jtf_ip.getText(), this.jtf_netMask.getText(), this.jtf_gateway.getText()
                    );
                }
                sf.addCmd(formater1.toString());
                isOk = true;
            }
            if(!this.jtf_masterDns.getText().isEmpty()) {
                Formatter formater2 = new Formatter();
                formater2.format(
                        "netsh interface ip set dns name=%s source=%s addr=%s",
                        this.jtf_devName.getText(), this.jtf_networkType.getText(), this.jtf_masterDns.getText()
                );
                sf.addCmd(formater2.toString());
                isOk = true;
            }
            if(!this.jtf_backupDns.getText().isEmpty()) {
                Formatter formater3 = new Formatter();
                formater3.format(
                        "netsh interface ip add dns %s addr=%s index=2",
                        this.jtf_devName.getText(), this.jtf_backupDns.getText()
                );
                sf.addCmd(formater3.toString());
                isOk = true;
            }
        }
        if(this.jtf_networkType.getText().equals("dhcp")){
            Formatter formater1 = new Formatter();
            formater1.format(
                    "netsh interface ip set address name=%s source=%s",
                    this.jtf_devName.getText(), this.jtf_networkType.getText()
            );
            sf.addCmd(formater1.toString());
            Formatter formater2 = new Formatter();
            formater2.format(
                    "netsh interface ip set dns name=%s source=%s",
                    this.jtf_devName.getText(), this.jtf_networkType.getText()
            );
            sf.addCmd(formater2.toString());
            isOk = true;
        }
        if(isOk){
            this.setVisible(false);
        }
    }//GEN-LAST:event_jb_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JButton jb_save;
    private javax.swing.JTextField jtf_backupDns;
    private javax.swing.JTextField jtf_devName;
    private javax.swing.JTextField jtf_gateway;
    private javax.swing.JTextField jtf_ip;
    private javax.swing.JTextField jtf_masterDns;
    private javax.swing.JTextField jtf_netMask;
    private javax.swing.JTextField jtf_networkType;
    // End of variables declaration//GEN-END:variables
}
